﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Xeml.Document.Contracts;
using Xeml.Document;
using System.Collections.ObjectModel;

namespace Xid.Controls
{
    public partial class HirachicalViewCtrl : UserControl, INotifyPropertyChanged
    {
        private IOntologyTerm _selectedTerm;
        public IOntologyTerm SelectedTerm
        {
            get
            {
                return _selectedTerm;
            }
        }
        public HirachicalViewCtrl()
        {
            InitializeComponent();
        }

        public void Setup(IOntologyHandler view)
        {
            TreeNode tn = new TreeNode(view.ComponentName);
            tn.ImageIndex = 0;
            if (view is IHierarchicalView)
            {
                SortedDictionary<string, TermNode> list = new SortedDictionary<string, TermNode>();
                foreach (var item in (view as IHierarchicalView).Nodes)
                {
                    if (item.IsContainer)
                        list.Add(item.Label, item);
                    else
                        list.Add(item.Term.Prototype.Name, item);
                }

                foreach (var item in list.Values)
                {
                    if (!item.IsContainer && item.Term.Attributes.Contains("is_obsolete"))
                    {
                    }
                    else
                    {
                        TreeNode subnode = new TreeNode(item.Label);

                        subnode.Tag = item;
                        if (item.IsContainer)
                        {
                            subnode.ImageIndex = 1;
                            subnode.SelectedImageIndex = 1;
                        }
                        else
                        {
                            subnode.ImageIndex = 2;
                            subnode.SelectedImageIndex = 2;
                        }

                        tn.Nodes.Add(subnode);
                        AddNodes(subnode, item.Childs);
                    }
                }
            }

            treeView1.Nodes.Add(tn);
            tn.Expand();
        }

        private void AddNodes(TreeNode node, ReadOnlyCollection<AttributedRelation<TermNode>> childs)
        {
            SortedDictionary<string, AttributedRelation<TermNode>> list = new SortedDictionary<string, AttributedRelation<TermNode>>();
            foreach (var item in childs)
            {
                list.Add(item.Relation.Term.Prototype.Name, item);
            }


            foreach (var item in list.Values)
            {
                if (!item.Relation.IsContainer && item.Relation.Term.Attributes.Contains("is_obsolete"))
                {
                }
                else
                {

                    TreeNode subnode = new TreeNode(item.Relation.Label);
                    subnode.Tag = item;
                    if (item.Relation.IsContainer)
                    {
                        subnode.ImageIndex = 1;
                        subnode.SelectedImageIndex = 1;
                    }
                    else if (item.Attribute == "is_a")
                    {
                        subnode.ImageIndex = 3;
                        subnode.SelectedImageIndex = 3;
                    }
                    else if (item.Attribute == "part_of")
                    {
                        subnode.ImageIndex = 4;
                        subnode.SelectedImageIndex = 4;
                    }
                    else if (item.Attribute == "develops_from")
                    {
                        subnode.ImageIndex = 5;
                        subnode.SelectedImageIndex = 5;
                    }
                    else
                    {
                        subnode.ImageIndex = 2;
                        subnode.SelectedImageIndex = 2;
                        subnode.ToolTipText = item.Attribute;
                    }
                    node.Nodes.Add(subnode);

                    AddNodes(subnode, item.Relation.Childs);
                }
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            AttributedRelation<TermNode> tn = e.Node.Tag as AttributedRelation<TermNode>;
            TermNode tn2 = e.Node.Tag as TermNode;

            if (tn != null && !tn.Relation.IsContainer)
                _selectedTerm = tn.Relation.Term;
            else if (tn2 != null && !tn2.IsContainer)
                _selectedTerm = tn2.Term;
            else
                _selectedTerm = null;



            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("SelectedTerm"));
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
